package com.example.goodsprovider.controller;

import com.example.goodsprovider.mapper.CartMapper;
import com.example.goodsprovider.mapper.GoodsMapper;
import com.myshop.b2b.commonmodule.Cart;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author likaiping
 * @createTime 2024/7/3 15:38
 */
@RestController
@AllArgsConstructor
public class CartController {
    private CartMapper cartMapper;
    private GoodsMapper goodsMapper;

    @GetMapping("/deleteCart")
    public int deleteCart(@RequestParam("gid")int gid){
        Cart cart = cartMapper.selectAllById(gid);
        goodsMapper.updateGoodsStock(cart.getGoodid(),cart.getNumber());
        return cartMapper.deleteCart(gid);
    }
    @GetMapping("/insertOrder")
    public int insertOrder(@RequestParam("goodsname")String goodsname,
                           @RequestParam("number")int number,
                           @RequestParam("price")int price,
                           @RequestParam("uid")int uid){
        return cartMapper.insertOrder(goodsname,number,price,uid);
    }

    //老师的ShoppingController部分
    @GetMapping("/updateCart")
    public int updateCart(@RequestParam("number")int number, @RequestParam("id")int id){
        return cartMapper.updateCart(number,id);
    }
    @GetMapping("/getAllcart")
    public List<Cart> getAllcart(@RequestParam(value = "uid")int uid){
        return cartMapper.getAllcart(uid);
    }
    @GetMapping("/intcart")
    public int intcart(@RequestParam("goodsname")String goodsname,
                       @RequestParam("number")int number,
                       @RequestParam("price")int price,
                       @RequestParam("goodid")int goodid,
                       @RequestParam("uid")int uid){
        return cartMapper.intcart(goodsname,number,price,goodid,uid);
    }

    /**
     * 单次修改购物车商品数量
     * @param number
     * @param id
     * @return
     */
    @GetMapping("/editCartNum")
    public boolean updateCartNumber(@RequestParam("number")int number,@RequestParam("id")int id){
        return cartMapper.updateCartNumber(number,id);
    }
}
